package com.vercon.moensmart.view

import android.view.View
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView

class ViewPagerSnapHelper : PagerSnapHelper() {
 
    override fun findTargetSnapPosition(layoutManager: RecyclerView.LayoutManager, velocityX: Int, velocityY: Int): Int {
        val position = super.findTargetSnapPosition(layoutManager, velocityX, velocityY)
        if (position >= 0) {
            listener?.onSnapFromFling(position)
        }
        return if (position >= layoutManager.itemCount) {
             0
        } else {
            super.findTargetSnapPosition(layoutManager, velocityX, velocityY)
        }
    }

    var listener : OnSnapListener? = null
    fun setOnSnapListener(l : OnSnapListener){
        listener = l
    }
    interface OnSnapListener {
        fun onSnapFromFling(position: Int)
        fun onFinalSnap(view: View, position: Int)
    }
}